package com.itany.hotel.action;

import com.itany.hotel.entity.Member;
import com.itany.hotel.entity.Room;
import com.itany.hotel.entity.Subscription;
import com.itany.hotel.service.SubscriptionService;
import framework.bean.factory.ObjectFactory;
import framework.mvc.annotation.Action;
import framework.mvc.annotation.Controller;
import framework.mvc.annotation.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Controller
@Action(path = "/subscription")
public class SubscriptionAction {


    //根据用户提交的
    // 客房类型
    // 预定方式
    // 开始日期
    // 结束日期
    //查询可用房间
    //页面处理
    //  开始日期和结束日期需要使用js校验，日期需要在今天之后，开始日期在结束日期之前
    //  房间类型是查询出来的
    public String search(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        //获得客房类型，预定方式，开始日期，结束日期
        //获得subscriptionId
        String subscriptionId=request.getParameter("subscriptionId");

        //调用业务层查询可用房间
        List<Room> rooms=null;//????????????

        request.setAttribute("rooms",rooms);
        request.setAttribute("subscriptionId",subscriptionId);
        request.setAttribute("预定方式，开始日期，结束日期",subscriptionId);

        Random random=new Random();
        request.getSession().setAttribute("token",random.nextInt(9999)+"");

        return "success";
    }


    //创建明细
    public String createDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        HttpSession session=request.getSession();

        String token=request.getParameter("token");
        String sessionToken= (String) session.getAttribute("token");

        //判断token是否正确，如果不正确
            //提示用户
            //return


        //获得选择的房间id（多个）
        //获得预定方式，开始日期，结束日期，subscriptionId

        //调用业务层创建明细


        session.removeAttribute("token");


        return "success";

    }




















        @Action(path = "/check/findUnacknowledged", results = {
            @Result(path = "/WEB-INF/page/memberCenter.jsp")
    })
    public String findUnacknowledged(HttpServletRequest request, HttpServletResponse response) throws ServletException {

        SubscriptionService subscriptionService = (SubscriptionService) ObjectFactory.getObject("subscriptionService");


        Member member = (Member) request.getSession().getAttribute("member");

        List<Subscription> subscriptions = subscriptionService.findUnacknowledged(member.getId());

        request.setAttribute("subscriptions", subscriptions);

        return "success";

    }
}